<?php

/**
 * @author FROSTY (valik619)
 * @site nadmad.ru
 * @mail valik619@inbox.ru
 */
 
$title = 'WBCMS - wap blogs CMS';
$root = '../';
include('../system/core.php');

if (!$uid) {
    header('Location: http://'.$set['home'].'/login.php');
    exit;
}

if ($user['rights'] != 1) {
    header('Location: http://'.$set['home'].'/index.php');
    exit;
}

include('../system/head.php');

$action=htmlspecialchars(trim($_GET['a']));

echo '<div class="title">'.$lang['counters'].'</div>';

switch ($action){

default:

echo '<div id="imenu">';

echo '<div class="info">';

echo '<form action="banners.php?a=add" method="post">';
echo "$lang[title]<br />";
echo '<input type="name" name="name"></input><br />';
echo "$lang[cod_countera] $lang[code_general]<br />";
echo '<textarea name="code"></textarea><br />';
echo "$lang[cod_countera] $lang[code_other]<br />";
echo '<textarea name="code1"></textarea><br />';
echo ''.$lang['code_general_ok'].'</small><br /><input type="checkbox" name="acces" value="1">Yes</p>';  
echo '<p><input type="submit" name="submit" value="'.$lang['add'].'"/></form></p>';
echo "$lang[code_info]<br />";
echo '<p><a href="banners.php?a=all">'.$lang['all'].' '.$lang['counters'].'</a></p>';
    

echo '</div>'; // class=info

echo '</div>'; //id=imenu

break;


case 'add':
$code1 = $_POST['code1'];
//Это код для других страниц
$code = $_POST['code'];
//код главной
$name = $_POST['name'];
//Имя для отображения в настройках
$yes = $_POST['acces'];
if(!$code){
    echo 'ERROR! CODE FOR GENERAL PAGE NOT EXIST`S'; break;
}
if(!$code1 && $yes != 1){
    echo 'ERROR! CODE FOR OTHER PAGES NOT EXIST`S'; break;
}
if(!$name || strlen($name) < 5 || strlen($name) > 50){
    echo 'ERROR! Name not correct! Min = 5 AND Max = 50'; break;
}

if (isset($_POST['save'])) { 
    mysql_query("INSERT INTO `counters` (`id`,`name`, `code`, `code1`, `general`) VALUES (NULL ,'$name','$code', '$code1', '$yes');");
    echo '<div class="small"><a href="banners.php" class="url"><span class="green">Success!</span></a></div>';
    break;
    }

//$code = mysql_real_escape_string($code);

echo '<div id="imenu">';
echo '<div class="info">';
echo '<p>For general: <br /><br />'.$code.'</p><br />';
if($yes == 0){
echo '<br /><p>For others pages: <br /><br />'.$code1.'</p>';
}
echo '<form action="banners.php?a=add" method="post">';
echo "$lang[title]<br />";
echo '<input type="name" name="name" value="'.$name.'"></input><br />';
echo "$lang[cod_countera] $lang[code_general]<br />";
echo '<textarea name="code">'.$code.'</textarea><br />';
echo "$lang[cod_countera] $lang[code_other]<br />";
echo '<textarea name="code1">'.$code1.'</textarea><br />';
if($yes == 1){
echo ''.$lang['code_general_ok'].'</small><br /><input type="checkbox" name="acces" value="1" checked>Yes</p>';
}else{
echo ''.$lang['code_general_ok'].'</small><br /><input type="checkbox" name="acces" value="1">Yes</p>';
}
echo '<p><input type="submit" name="save" value="'.$lang['add'].'"/> | <input type="submit" name="submit" value="UPDATE"/></form></p>';
echo '</div>';
echo '</div>';



break;

case 'all': 

$posts=mysql_query("SELECT * FROM `counters` ORDER BY `id` DESC");

    //Моя навигация
    $page = (isset($_GET['p'])) ? intval($_GET['p']) : 1; 
    $allpage = ceil(mysql_num_rows($posts)/$kmess);
    $start = $page*$kmess - $kmess; 

echo '<div id="imenu">';

if(!mysql_num_rows($posts)){
    echo '<div class="info">'.$lang['counters_not'].'</div>';
        
        }else{
          $post=mysql_query("SELECT * FROM `counters` ORDER BY `id` DESC LIMIT $start,$kmess");
while($row=mysql_fetch_assoc($post)){
    
    echo '<div class="info"><a href="banners.php?a=edit&amp;id='.$row['id'].'">'.$row['name'].'</a> <a href="banners.php?a=delete&amp;id='.$row['id'].'" style="float: right;">X</a></div>';
    
    $type = explode(".", $row['name']);
    $type = $type[count($type) - 1];
    
    };
    $total = mysql_num_rows($posts);
    if($total>$kmess){
    echo '<p>';
    functions::navigation($allpage,"banners.php?a=all&amp;p={p}",1);  
echo '</p>';}
}
echo '</div>';

echo '<div class="title"><a href="banners.php">'.$lang['counters'].'</a></div>';
break;



case 'edit':
$pr=$_GET['id'];
if(!$pr){
    echo 'ERROR! ID NOT EXISTS'; break;
}
$row1 = mysql_query("SELECT * FROM `counters` WHERE `id`='$pr'");
$code1 = mysql_fetch_assoc($row1);
if (isset($_POST['submit']) || isset($_POST['save'])) { 
    $name = $_POST['name'];
    $code= $_POST['code'];
    $code1= $_POST['code1'];
    $yes= $_POST['acces'];
}else{
$name = $code1['name'];
$code=$code1['code'];
$code1=$code1['code1'];
$yes=$code1['general'];
}
if(!$code){
    echo 'ERROR! CODE NOT EXIST`S'; break;
}
if(!$name || strlen($name) < 5 || strlen($name) > 50){
    echo 'ERROR! Name not correct! Min = 5 AND Max = 50'; break;
}

if (isset($_POST['save'])) { 
    mysql_query("UPDATE `counters` SET `code` = '$code' WHERE `id` = '$pr'");
    mysql_query("UPDATE `counters` SET `name` = '$name' WHERE `id` = '$pr'");
    mysql_query("UPDATE `counters` SET `general` = '$yes' WHERE `id` = '$pr'");
    mysql_query("UPDATE `counters` SET `code1` = '$code1' WHERE `id` = '$pr'");
    echo '<div class="small"><a href="banners.php?a=all" class="url"><span class="green">Success!</span></a></div>';
    break;
    }
    

echo '<div id="imenu">';
echo '<div class="info">';
echo '<p>For general: <br /><br />'.$code.'</p><br />';
if($yes == 0){
echo '<br /><p>For others pages: <br /><br />'.$code1.'</p>';
}
echo '<form action="banners.php?a=edit&amp;id='.$pr.'" method="post">';
echo "$lang[title]<br />";
echo '<input type="name" name="name" value="'.$name.'"></input><br />';
echo "$lang[cod_countera] $lang[code_general]<br />";
echo '<textarea name="code">'.$code.'</textarea><br />';
echo "$lang[cod_countera] $lang[code_other]<br />";
echo '<textarea name="code1">'.$code1.'</textarea><br />';
if($yes == 1){
echo ''.$lang['code_general_ok'].'</small><br /><input type="checkbox" name="acces" value="1" checked>Yes</p>';
}else{
echo ''.$lang['code_general_ok'].'</small><br /><input type="checkbox" name="acces" value="1">Yes</p>';
}
echo '<p><input type="submit" name="save" value="Save"/> | <input type="submit" name="submit" value="UPDATE"/></form></p>';
echo '</div>';
echo '</div>';



break;


case 'delete':
$pr=$_GET['id'];

$zapros = mysql_query("SELECT * FROM `counters` WHERE `id` = '$pr'");
if(mysql_num_rows($zapros)){
    mysql_query("DELETE FROM `counters` WHERE `id` = '$pr'");
    echo '<div class="small"><a href="banners.php?a=all" class="url"><span class="green">Saccess!</span></a></div>';
}else{
  echo '<div class="small"><a href="banners.php?a=all" class="url"><span class="red">ERROR! THIS COUNTER DOESN`T EXIST`S</span></a></div>';  
}
break;


}


include('../system/foot.php');

/**
 * @author FROSTY (valik619)
 * @site nadmad.ru
 * @mail valik619@inbox.ru
 */

?>